import { showSourceDetail } from '@/api/base.js'

// 库存管理-移库消耗产品和批次选择输入框焦点方法
export function setVal(className) {
  let dom = document
    .getElementsByClassName(className)[0]
    .querySelector('.el-input__inner')
  let val = dom.value
  dom.focus()
  dom.value = ''
  setTimeout(() => {
    dom.value = val
  }, 0)
}

export function setTabindex(proxy) {
  proxy.$nextTick(() => {
    let originalInptDom = document.getElementsByClassName(
      'el-checkbox__original'
    )
    originalInptDom.forEach((element) => {
      element.setAttribute('tabindex', -1)
      element.setAttribute('value', 1)
    })
  })
}

export function toSourceDetail(proxy, id, businessId, routeQuery) {
  showSourceDetail(id).then((res) => {
    if (res && res.success) {
      if (businessId) {
        res.data['businessId'] = businessId
        res.data['orderId'] = businessId
      }
      // 如果接口返回则使用返回orderId
      if (res.data.orderId) {
        res.data['businessId'] = res.data.orderId
        res.data['orderId'] = res.data.orderId
      }
      res.data['orderNo'] = id
      turnPage(proxy, res.data, routeQuery)
    }
  })
}

function turnPage(proxy, data, routeQuery = {}) {
  let url = data.url
  let name = ''
  let routes = proxy.$store.state.permission.routes
  routes.map((item) => {
    if (item.children && item.children.length) {
      item.children.map((i) => {
        if (i.url === url) {
          name = url
        }
      })
    }
  })

  if (name) {
    proxy.$router.push({
      path: url,
      query: routeQuery
    })
    proxy.$store.dispatch('settings/toPage', data)
  } else {
    proxy.$message({
      duration: 3500,
      type: 'warning',
      showClose: true,
      message: '你没有相应的菜单权限',
    })
  }
}


// 限制当前日期 时分秒之前时间禁用
export const disableDate = (time) => {
  return time.getTime() < Date.now() - 8.64e7
}

/* 限制小时 */
export const disabledHour = () => {
  const arrs = []
  for (let i = 0; i < 24; i++) {
    if (new Date().getHours() <= i) continue
    arrs.push(i)
  }
  return arrs
}

/* 限制分 */
export const disabledMinute = () => {
  const arrs = []
  for (let i = 0; i < 60; i++) {
    if (new Date().getMinutes() <= i) continue
    arrs.push(i)
  }
  return arrs
}

/* 限制秒 */
export const disabledSecond = () => {
  const arrs = []
  for (let i = 0; i < 60; i++) {
    if (new Date().getSeconds() <= i) continue
    arrs.push(i)
  }
  return arrs
}
// 设置选中时间之前日期
export function setDisDate(val, row) {
  row.disableDate = (time) => {
    return time.getTime() < new Date(val).getTime() - 8.64e7
  }
  row.disabledHour = () => {
    const arrs = []
    for (let i = 0; i < 24; i++) {
      if (new Date(val).getHours() <= i) continue
      arrs.push(i)
    }
    return arrs
  }

  row.disabledMinute = () => {
    const arrs = []
    for (let i = 0; i < 60; i++) {
      if (new Date(val).getMinutes() <= i) continue
      arrs.push(i)
    }
    return arrs
  }

  row.disabledSecond = () => {
    const arrs = []
    for (let i = 0; i < 60; i++) {
      if (new Date(val).getSeconds() <= i) continue
      arrs.push(i)
    }
    return arrs
  }
}